//
//  ResumeModifyCtrl.m
//  laowu
//
//  Created by Northwan on 15/5/3.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "ResumeModifyCtrl.h"
#import "CustomSpannerView.h"
#import "HomeDataTool.h"
#import "LocationDao.h"
#import "AddressDbBean.h"


@interface ResumeModifyCtrl ()<UITextFieldDelegate,UIScrollViewDelegate,CustomSpannerViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headImgView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextField *provinceEdit;
@property (weak, nonatomic) IBOutlet UITextField *cityEdit;
@property (nonatomic,weak)UIView *currentEditView;
@property (nonatomic,assign)CGFloat moveDis;//移动距离
@property (nonatomic,assign)CGFloat keyboardHeight;//键盘高度，如果为0，代表当前不是编辑模式
- (IBAction)enterModify:(id)sender;
- (IBAction)workStatusBtnClick:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *workTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *techDegreeLabel;
@property (weak, nonatomic) IBOutlet UILabel *hopeSalaryLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *identifyLabel;
@property (weak, nonatomic) IBOutlet UILabel *telLabel;
@property(strong,nonatomic)ResumeRes *result;


@property (nonatomic, strong) NSArray *provincesArray;
@property (nonatomic, strong) NSArray *cityArray;
@end

@implementation ResumeModifyCtrl


+(instancetype)initWithData:(ResumeRes *)result{
    ResumeModifyCtrl* object = [[ResumeModifyCtrl alloc]init];
    object.result = result;
    return object;
}

-(NSArray *)provincesArray{
    if (!_provincesArray) {
        LocationDao *dao = [[LocationDao alloc] init];
        _provincesArray = [dao getProvinces];
    }
    return _provincesArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"修改简历"];
    CALayer *layer=[self.headImgView layer];
    [layer setCornerRadius:self.headImgView.width/2];
    [layer setMasksToBounds:YES];
    [layer setBorderWidth:2];
    [layer setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.scrollView setAlwaysBounceVertical:YES];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap)];
    [self.scrollView addGestureRecognizer:tap];
    [self.scrollView setDelegate:self];
    [self.provinceEdit setDelegate:self];
    [self.cityEdit setDelegate:self];
    [self registNotice];
}

-(void)viewWillAppear:(BOOL)animated{
    [_nameLabel setText:_result.user_name];
    [_identifyLabel setText:_result.identity_card];
    [_telLabel setText:_result.telnum];
    [_workTypeLabel setText:_result.work_type];
    [_techDegreeLabel setText:_result.work_skill_level];
    [_hopeSalaryLabel setText:_result.salary];
    [_provinceEdit setText:_result.province];
    [_cityEdit setText:_result.city];
}

-(void)registNotice{
    //注册软键盘弹出隐藏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (BOOL)textFieldShouldBeginEditing:(UITextView *)textView{
    self.currentEditView = textView;
    if (self.keyboardHeight!=0) {
        [self adjustLocationWithKeyboardHeight:self.keyboardHeight];
    }
    return YES;
}

-(void)adjustLocationWithKeyboardHeight:(CGFloat)keyboardHeight{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    CGRect editViewRect = [self.currentEditView convertRect:self.currentEditView.bounds toView:window];
    
    //首页判断键盘有没有遮挡view，如果没有遮挡，那个什么也不做
    CGFloat viewMaxY = CGRectGetMaxY(editViewRect);
    if (viewMaxY+10>SCREENH-keyboardHeight) {
        //代表键盘遮挡了view
        //需要调整uiscrollview的contentoffset移动
        self.moveDis = (viewMaxY+10)-(SCREENH-keyboardHeight);
        [UIView animateWithDuration:0.25 animations:^{
            [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y+self.moveDis)];
        }];
    }
}

- (void)keyboardWillShow:(NSNotification *)notif
{
    //在键盘显示的时候去调整view
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    self.keyboardHeight = keyboardSize.height;
    [self adjustLocationWithKeyboardHeight:keyboardSize.height];
}

-(void)keyboardWillHide:(NSNotification*)notif{
    self.keyboardHeight = 0;
    self.currentEditView = nil;
    if (self.scrollView.contentOffset.y!=0) {
        [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y-self.moveDis)];
    }
    self.moveDis = 0;
}

- (IBAction)enterModify:(id)sender {


    NSString *province = _provinceEdit.text;
    NSString *city = _cityEdit.text;
    NSString *workType =  _workTypeLabel.text;
    NSString *techLevel =  _techDegreeLabel.text;
    NSString *salary = _hopeSalaryLabel.text;
    if(ISBLANK(workType)){
        [DialogUtils showTipsWithView:self.view String:@"工种为空"];
        return;
    }
    if(ISBLANK(techLevel)){
        [DialogUtils showTipsWithView:self.view String:@"等级为空"];
        return;
    }
    if(ISBLANK(salary)){
         [DialogUtils showTipsWithView:self.view String:@"薪资为空"];
        return;
    }
    if(ISBLANK(province)){
        [DialogUtils showTipsWithView:self.view String:@"地区省份为空"];
        return;
    }
    if(ISBLANK(city)){
        [DialogUtils showTipsWithView:self.view String:@"地区城市为空"];
        return;
    }
    [HomeDataTool setResumeInfo:workType techLevel:techLevel salary:salary province:province city:city success:^(BaseResponse *result) {
        if(result.isSucc){
            [[NSNotificationCenter defaultCenter]postNotificationName:@"notif_modify_resume_success" object:nil];
            [DialogUtils showTipsWithView:self.view String:@"修改成功"];
        }else{
            [DialogUtils showTipsWithView:self.view String:result.msg];
        }
    } failure:^(NSError *error) {
         [DialogUtils showTipsWithView:self.view String:@"修改失败"];
    }];
    NSLog(@"enterModify");
}

- (IBAction)workStatusBtnClick:(UIButton *)sender {
    
    BOOL istop = NO;
    NSArray *stringArray = STRING_ARRAY_WORKTYPE_STATUS;
    switch (sender.tag) {
        case 0:
            stringArray = STRING_ARRAY_WORKTYPE_STATUS;
            break;
        case 1:
            stringArray = STRING_ARRAY_TECHDEGREE_STATUS;
            break;
        case 2:
            stringArray = STRING_ARRAY_HOPESALARY_STATUS;
            istop = YES;
            break;
        default:
            break;
    }
    CustomSpannerView *spannerView = [[CustomSpannerView alloc] initWithStringArray:stringArray bearingView:sender];
    [spannerView setDelegate:self];
    spannerView.isTop = istop;
    [spannerView show];

}

-(void)customSpannerItemClick:(NSString *)title index:(NSInteger)index bearingView:(UIView *)bearingView{
    if (bearingView.tag == 0) {
        [self.workTypeLabel setText:title];
    }else if(bearingView.tag == 1){
        [self.techDegreeLabel setText:title];
    }else if(bearingView.tag == 2){
        [self.hopeSalaryLabel setText:title];
    }else if(bearingView.tag == 3){
        //省
        [self.provinceEdit setText:title];
        //设置市
        AddressDbBean *bean = self.provincesArray[index];
        LocationDao *dao = [LocationDao new];
        self.cityArray = [dao getCitys:bean.provinceId];
        [self.cityEdit setText:[[self.cityArray firstObject] cityName]];
    }else if(bearingView.tag == 4){
        //市
        [self.cityEdit setText:title];
    }
}

- (IBAction)provincesBtnClick:(UIButton *)sender {
    NSArray *proArrayStr = [self.provincesArray valueForKeyPath:@"provinceName"];
    CustomSpannerView *spannerView = [[CustomSpannerView alloc] initWithStringArray:proArrayStr bearingView:sender];
    [spannerView setDelegate:self];
    [spannerView setIsTop:YES];
    [spannerView show];
}

- (IBAction)cityBtnClick:(UIButton *)sender {
    NSArray *cityArrayStr = [self.cityArray valueForKeyPath:@"cityName"];
    CustomSpannerView *spannerView = [[CustomSpannerView alloc] initWithStringArray:cityArrayStr bearingView:sender];
    [spannerView setDelegate:self];
    [spannerView setIsTop:YES];
    [spannerView show];
}

-(void)actionTap{
    [self.view endEditing:YES];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


@end
